package IO;

import java.io.*;

/**
 * 缓冲流
 * @author 15594
 */
public class BufferedInOutputStream {
    public static void main(String[] args) {
        //注意：读取的文件必须确定是存在的
        File file = new File("C:\\Users\\15594\\Desktop\\01-视频.avi");
        //注意：写入的文件可以不存在，在写入时若文件不存在就会创建文件
        File file1 = new File("C:\\Users\\15594\\Desktop\\02-视频-copy.avi");
        copy(file,file1);
    }

    private static void copy(File file, File file1) {

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            FileInputStream inputStream = new FileInputStream(file);
            FileOutputStream outputStream = new FileOutputStream(file1);
            bis = new BufferedInputStream(inputStream);
            bos = new BufferedOutputStream(outputStream);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = bis.read(bytes))!=-1){
                bos.write(bytes,0,len);

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
